package com.superhelp.ebill.activity.login.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.superhelp.ebill.R;
import com.superhelp.ebill.activity.base.FFBaseActivity;
import com.superhelp.ebill.activity.login.presenter.LoginPresenter;
import com.superhelp.ebill.activity.login.presenter.LoginPresenterImplementation;
import com.superhelp.ebill.api.ResultCode;
import com.superhelp.ebill.api.result.LoginResult;
import com.superhelp.ebill.util.FFToast;
import com.superhelp.ebill.util.StringUtils;
import com.superhelp.ebill.util.UserPrefUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import zuo.biao.library.interfaces.OnBottomDragListener;

/**
 * 登陆
 */
public class LoginActivity extends FFBaseActivity implements OnBottomDragListener, LoginView {

    @BindView(R.id.btn_register)
    LinearLayout btnRegister;
    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.tv_findpsw)
    RelativeLayout tvFindpsw;
    private RelativeLayout tvFindPsw;
    private UserPrefUtils userPrefUtil;

    private LoginPresenter mUserLoginPresenter = new LoginPresenterImplementation(this);

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, LoginActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ff_activity_login, this);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    public void initView() {
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        tvFindPsw = (RelativeLayout) findViewById(R.id.tv_findpsw);
    }

    @Override
    public void initData() {
        userPrefUtil = new UserPrefUtils(this);
    }

    @Override
    public void initEvent() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return false;
    }

    @Override
    public void onDragBottom(boolean rightToLeft) {

    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void showLoading() {
        showProgressDialog("正在登录");
    }

    @Override
    public void hideLoading() {
        dismissProgressDialog();
    }

    @Override
    public void onHttpError(Throwable throwable) {
        ResultCode.toastVolleyError(getActivity(), throwable);
    }

    @Override
    public void onHttpSuccess(LoginResult loginResult) {
        if (loginResult.getCode() == ResultCode.SUCCESS) {
            FFToast.makeText(getActivity(), "登录成功", Toast.LENGTH_SHORT).show();
        }
    }

    @OnClick({R.id.btn_register, R.id.btn_login, R.id.tv_findpsw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_register:
                break;
            case R.id.btn_login:
                if ("".equals(username.getText().toString().trim())) {
                    FFToast.makeText(this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("".equals(password.getText().toString().trim())) {
                    FFToast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!StringUtils.isPhoneNum(username.getText().toString().trim())) {
                    FFToast.makeText(this, "手机格式不正确", Toast.LENGTH_SHORT).show();
                    return;
                }
                mUserLoginPresenter.login(username.getText().toString().trim(), password.getText().toString().trim());
                break;
            case R.id.tv_findpsw:
                break;
        }
    }
}
